<%@ page contentType="text/html; charset=utf-8"%>

<%@ include file="/WEB-INF/jsp/includeTaglib.jsp" %>

<form id="pageForm">
	<input type="hidden" name="pageDirection" value="">

	<div align="right" class="globalText">

		    <c:if test="${isFirstPage}">上一页</c:if>
		    <c:if test="${!isFirstPage}">
		      <input type="hidden" name="nextPageIndex" value="${previousPageIndex}">
		      <input type="hidden" name="recordStartIndex" value="${previousRecordStartIndex}">
					<a href="javascript:void(0)" id="goPreviousPage.htm">上一页</a>
		    </c:if>
		    &nbsp;&nbsp;&nbsp;
		    <c:if test="${hasPreviousPageArea}"><a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=1">1</a>&nbsp;<a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${pageArea[0]-1}">...</a></c:if><c:forEach var="page" items="${pageArea}"><c:choose><c:when test="${page == nextPageIndex}"><span class="ft3"><strong>${page}</strong></span>&nbsp;</c:when><c:otherwise><a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${page}">${page}</a>&nbsp;</c:otherwise></c:choose><c:set var="maxAreaPage" value="${page}"/></c:forEach><c:if test="${hasNextPageArea}"><a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${maxAreaPage+1}">...</a>&nbsp;<a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${pageCount}">${pageCount}</a></c:if>
		    &nbsp;&nbsp;&nbsp;
		    <c:if test="${!isLastPage}">
		      <input type="hidden" name="nextPageIndex" value="${nextPageIndex}">
		      <input type="hidden" name="recordStartIndex" value="${nextRecordStartIndex}">
					<a href="javascript:void(0)" id="goNextPage.htm">下一页</a>
		    </c:if>
		    <c:if test="${isLastPage}">下一页</c:if>
				<input type="text" size="4" name="pageTxt" />&nbsp;
				<a href="javascript:void(0)" id="goInputPage.htm">GO</a>
			</div>

</form>

<script type="text/javascript">
//取得正欲跳转前往的url
	var lookupPathForRequest = "<c:url value='${lookupPathForRequest}'/>";

	$(document).ready(function() {
    var pageForm = $("form[id=pageForm]");

		//前一页
		pageForm.find("a[id*=goPreviousPage.htm]").click(function(){
	    var pageDirection = "previous";
	    turnToPage(pageDirection);
			return false;
		});

		//翻转到特定页码(用户选择)
		pageForm.find("a[id*="+lookupPathForRequest+"?page]").click(function(){
			var pageNum = getUrlParamOfHrefId($(this));
	    turnToPage(pageNum);
			return false;
		});

		//翻转到特定页码(文本输入，点击Go)
		pageForm.find("a[id*=goInputPage.htm]").click(function(){
    	var pageForm = $("form[id=pageForm]");
    	var pageTxt = pageForm.find("input[name=pageTxt]").val();

    	if (!validatePositiveInteger(pageTxt)) {        	
    		alert("页码输入无效");
				return false;
    	}
	    turnToPage(parseInt(pageTxt));
			return false;
		});
		//翻转到特定页码(文本输入，回车键入)
    var pageTxt = pageForm.find("input[name=pageTxt]");
    pageTxt.keypress(function(e) {
    	if (e.which == 13) {//回车按下
    		var page =  $(this).val();
    		
	    	if (!validatePositiveInteger(page)) {
	    		alert("页码输入无效");
					return false;
	    	}
		    turnToPage(parseInt(page));
    		return false;
    	}
    });

		//后一页
		pageForm.find("a[id*=goNextPage.htm]").click(function(){
	    var pageDirection = "next";
	    turnToPage(pageDirection);
			return false;
		});

		//翻页主函数
		function turnToPage(page) {
	    var pageForm = $("form[id=pageForm]");
	    //pageForm.submit();
	    
	    //ajax implementaion here....

	    if (page == "next" || page == "previous") {
		    var nextPageIndex = pageForm.find(":hidden[name=nextPageIndex]").val();
		    var recordStartIndex = pageForm.find(":hidden[name=recordStartIndex]").val();

				if (pageForm.find(":hidden[name=nextPageIndex]").length == 1) {
					$.get(lookupPathForRequest, {pageDirection:page, nextPageIndex:nextPageIndex, recordStartIndex:recordStartIndex},
					  function(data){
						/*if (isGlobalErrorPopup($(data))) {
							openErrorDialog($(data));
							return;
						}*/
					    $("#ajaxpagetoolbar").replaceWith(data);
					  });
				} else if (pageForm.find(":hidden[name=nextPageIndex]").length > 1){
			    var nextPageIndexArr = pageForm.find(":hidden[name=nextPageIndex]");
			    var recordStartIndexArr = pageForm.find(":hidden[name=recordStartIndex]");

			    /*var pageIndex0 = nextPageIndexArr[0].value;
			    var pageIndex1 = nextPageIndexArr[1].value;
			    var recordStartIndex0 = recordStartIndexArr[0].value;
			    var recordStartIndex1 = recordStartIndexArr[1].value;*/

					$.get(lookupPathForRequest+"?pageDirection="+page+"&"+$.param(nextPageIndexArr, true)+"&"+$.param(recordStartIndexArr, true),
					  function(data){
						/*if (isGlobalErrorPopup($(data))) {
							openErrorDialog($(data));
							return;
						}*/
					    $("#ajaxpagetoolbar").replaceWith(data);
					  });
				}
	    } else {//翻到指定页
		    var currentPageIndex = page - 1;
		    var recordStartIndex = currentPageIndex * ${pageSize};//page size(maybe change)

				$.get(lookupPathForRequest, {nextPageIndex:currentPageIndex, recordStartIndex:recordStartIndex},
				  function(data){
					/*if (isGlobalErrorPopup($(data))) {
						openErrorDialog($(data));
						return;
					}*/
				    $("#ajaxpagetoolbar").replaceWith(data);
				  });
	    }
		}
	});
</script>